//
//  ZZAudioSettingViewController.m
//  RecorderTest
//
//  Created by 张彦东 on 2017/5/24.
//  Copyright © 2017年 Z. All rights reserved.
//

#import "ZZAudioSettingViewController.h"
#import "ZZSelector.h"
#import "ZZAudioParameter.h"
#import "ZZAudioManager.h"

#define kIdentifierChannels @"kIdentifierChannels"
#define kIdentifierSampleRate @"kIdentifierSampleRate"
#define kIdentifierLinearPCMBitDepth @"kIdentifierLinearPCMBitDepth"
#define kIdentifierAudioQuality @"kIdentifierAudioQuality"
#define kIdentifierEncoderBitRate @"kIdentifierEncoderBitRate"

@interface ZZAudioSettingViewController () <UITableViewDelegate, UITableViewDataSource, ZZSelectorDelegate>

@property (nonatomic, weak) UITableView *tableView;

@property (nonatomic, strong) NSArray<NSDictionary *> *params;

@end

@implementation ZZAudioSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UITableView *tableView = [[UITableView alloc] init];
    [self.view addSubview:tableView];
    _tableView = tableView;
    tableView.rowHeight = 44;
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    tableView.indicatorStyle = UIScrollViewIndicatorStyleDefault;
    tableView.accessibilityNavigationStyle = UIAccessibilityNavigationStyleSeparate;
    tableView.delegate = self;
    tableView.dataSource = self;
    [self updateInfo];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    _tableView.frame = self.view.bounds;
}

- (void)updateInfo {
    NSDictionary *channels = @{@"text" : @"声道",
                               @"detail" : [ZZAudioParameter channelsDisplayAtIndex:[ZZAudioManager manager].channelsIndex]};
    NSDictionary *sampleRate = @{@"text" : @"采样率",
                               @"detail" : [ZZAudioParameter sampleRateDisplayAtIndex:[ZZAudioManager manager].sampleRateIndex]};
    NSDictionary *linear = @{@"text" : @"比特率",
                                 @"detail" : [ZZAudioParameter linearPCMBitDepthDisplayAtIndex:[ZZAudioManager manager].linearPCMBitDepthIndex]};
    NSDictionary *quality = @{@"text" : @"声音质量",
                                 @"detail" : [ZZAudioParameter audioQualityDisplayAtIndex:[ZZAudioManager manager].audioQualityIndex]};
    NSDictionary *encoder = @{@"text" : @"编码比特率",
                                 @"detail" : [ZZAudioParameter encoderBitRateDisplayAtIndex:[ZZAudioManager manager].encoderBitRateIndex]};
    _params = @[channels, sampleRate, linear, quality, encoder];
    [_tableView reloadData];
}

#pragma mark -UITableViewDelegate UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_params count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellName = @"settingCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellName];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    cell.textLabel.text = _params[indexPath.row][@"text"];
    cell.detailTextLabel.text = _params[indexPath.row][@"detail"];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSArray *options;
    NSString *identifier;
    switch (indexPath.row) {
        case 0:
            options = [ZZAudioParameter channels];
            identifier = kIdentifierChannels;
            break;
        case 1:
            options = [ZZAudioParameter sampleRate];
            identifier = kIdentifierSampleRate;
            break;
        case 2:
            options = [ZZAudioParameter linearPCMBitDepth];
            identifier = kIdentifierLinearPCMBitDepth;
            break;
        case 3:
            options = [ZZAudioParameter audioQuality];
            identifier = kIdentifierAudioQuality;
            break;
        case 4:
            options = [ZZAudioParameter encoderBitRate];
            identifier = kIdentifierEncoderBitRate;
            break;
    }
    
    ZZSelector *selector = [[ZZSelector alloc] init];
    selector.delegate = self;
    selector.options = options;
    selector.identifier = identifier;
    [selector show];
}

#pragma mark - ZZSelectorDelegate
- (void)selector:(ZZSelector *)selector didSelectedRowAtIndex:(NSInteger)index identifier:(NSString *)identifier {
    if ([identifier isEqualToString:kIdentifierChannels]) {
        [ZZAudioManager manager].channelsIndex = index;
    } else if ([identifier isEqualToString:kIdentifierSampleRate]) {
        [ZZAudioManager manager].sampleRateIndex = index;
    } else if ([identifier isEqualToString:kIdentifierLinearPCMBitDepth]) {
        [ZZAudioManager manager].linearPCMBitDepthIndex = index;
    } else if ([identifier isEqualToString:kIdentifierAudioQuality]) {
        [ZZAudioManager manager].audioQualityIndex = index;
    } else if ([identifier isEqualToString:kIdentifierEncoderBitRate]) {
        [ZZAudioManager manager].encoderBitRateIndex = index;
    } else {
        NSLog(@"参数错误");
    }
    [self updateInfo];
}
@end
